<template>
	<view class="attend-container">
		<cc-navbar @height="navbarHeight"></cc-navbar>
		<u-sticky id="attend-filter">
			<view class="filter">
				<u-row gutter="8">
					<u-col span="4">
						<cc-date-picker :value.sync="query.date_range" @change="getList(1)"></cc-date-picker>
					</u-col>
					<u-col span="4">
						<cc-class-select :value.sync="query.cla_id" @complete="getList(1)" />
					</u-col>
					<u-col span="4">
						<cc-staff-select :value.sync="query.teacher_id" @complete="getList(1)" />
					</u-col>
				</u-row>
			</view>
			<cc-tabs :list="tabs" :scrollable="false" :current.sync="statusCurrent" @change="statusChange" />
		</u-sticky>

		<view class="list" v-if="count">
			<view v-for="row in list" :key="row.label">
				<view class="cc-p-x-32 cc-text-600 cc-text-32 cc-p-y-24 row-label" :style="{ top: labelOffsetTop }">
					{{ row.label }}{{ row.week }}
				</view>
				<view class="cc-p-x-24" v-for="(item, index) in row.list" :key="index">
					<cc-classtime :item="item" @complete="getList(1)"></cc-classtime>
				</view>
			</view>
		</view>
		<cc-loadmore show-limit="3" :loading="loading" :current="listNumber" :count="count" />
		<cc-empty v-if="!loading && !count" mode="list" />
	</view>
</template>

<script>
	import {
		classCourseList
	} from '@/codecat/api/education/class/classTime';
	import {
		getWeek
	} from '@/codecat/config/index.js';

	export default {
		computed: {
			listNumber() {
				return this.list.reduce((pre, cur) => {
					return Math.round(pre + cur.list.length);
				}, 0);
			}
		},
		data() {
			const endInitDate = this.$dayjs().format('YYYY-MM-DD');
			const beginInitDate = this.$dayjs().subtract(1, 'w').format('YYYY-MM-DD');
			return {
				labelOffsetTop: 0,
				loading: false,
				list: [],
				count: 0,
				query: {
					cla_id: null,
					teacher_id: '',
					date_range: [beginInitDate, endInitDate],
					status: null, // 状态 1 为，已上课，0 为未上课
					page: 1,
					limit: 10
				},

				statusCurrent: 0,
				tabs: [{
						name: '全部',
						value: null
					},
					{
						name: '待上课',
						value: 0
					},
					{
						name: '已上课',
						value: 1
					}
				]
			};
		},
		onLoad() {
			const options = this.$Route.query;
			this.query.status = options?.status === 0 ? options?.status : options?.status || null;
			this.query.cla_id = options?.cla_id || '';

			if (this.query.status !== null) {
				this.statusCurrent = this.query.status + 1;
			}
			this.getList(1);

			this.$store.dispatch('GetInfo');
		},
		onPullDownRefresh() {
			this.getList(1);
			uni.stopPullDownRefresh();
		},
		onReachBottom() {
			if (this.listNumber < this.count) {
				this.getList();
			}
		},
		methods: {
			navbarHeight(value) {
				this.$uGetRect('#attend-filter').then((size) => {
					this.labelOffsetTop = (value + size.height).toFixed(0) + 'px';
				});
			},
			async initPage() {
				const userInfo = await this.$store.dispatch('GetInfo');
				this.query.teacher_id = userInfo.teacher_id || '';
				this.getList(1);
			},
			statusChange(e) {
				this.query.status = e.value;
				this.getList(1);
			},
			getList(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				classCourseList(query)
					.then((res) => {
						this.query.page = query.page;
						this.loading = false;
						const {
							list,
							count
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							// this.list = this.list.concat(list);
							this.reorganize(list, this.list);
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			},
			/**
			 * @param {Array} target
			 * @param {Array} sourse
			 */
			reorganize(target, sourse) {
				target.map((e) => {
					const index = sourse.findIndex((s) => s.label === e.cla_date);
					if (index > -1) {
						sourse[index].list.push(e);
					} else {
						const week = getWeek(e.cla_date);
						const isToday = this.$dayjs(e.cla_date).isSame(new Date(), 'day');
						sourse.push({
							label: e.cla_date,
							week: isToday ? '(今天)' : `(周${week})`,
							list: [].concat(e)
						});
					}
				});
				return sourse;
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>